<cfsetting enablecfoutputonly="yes">

<cfparam name="attributes.url" default="#application.xtiurl#">
<cfparam name="attributes.dtd" default="#application.xtidtd#">
<cfparam name="attributes.debug" default="false">
<cfparam name="attributes.debuglevel" default="1,2,3">
<cfparam name="attributes.customerid">
<cfparam name="attributes.contactid" default="">
<cfparam name="attributes.crt_order" default="">
<cfparam name="attributes.order_number" default="">
<!--- <cfparam name="attributes.returnvar default=""> --->
<!--- ORDER HEADER --->
<cfparam name="attributes.misc_code_1" default="">
<cfparam name="attributes.misc_amt_1" default="">
<cfparam name="attributes.misc_code_2" default="">
<cfparam name="attributes.misc_amt_2" default="">
<cfparam name="attributes.source">
<cfparam name="attributes.ship_complete" default="">
<cfparam name="attributes.tsrid">
<cfparam name="attributes.tsr_taker" default="">
<cfparam name="attributes.user" default="web">
<cfparam name="attributes.client_order" default="">
<cfparam name="attributes.order_type" default="R">
<cfparam name="attributes.cid" default="">
<cfparam name="attributes.locid" default="">
<cfparam name="attributes.security_code" default="">
<cfparam name="attributes.currency" default="USD"> <!--- default to US Dollars if no value is provided --->

<!--- P02715 - allows updates of program_id and backorder flag values from order status screen --->
<cfparam name="attributes.program_id" default=""> 
<cfparam name="attributes.auto_bo" default="">
<cfif isdefined("attributes.cc_amt")>
	<cfset var.cc_amt = "<amount>" & attributes.cc_amt & "</amount>">
<cfelse>
	<cfset var.cc_amt = "">
</cfif>
<!--- P03285 Cybersource --->
<cfparam name="attributes.cus_browser_email" default=""><!--- placeholder only --->
<cfparam name="attributes.cus_cookies_accepted" default="Y"><!--- always true --->
<cfparam name="attributes.cus_ip_address" default=""><!--- placeholder only --->
<cfparam name="attributes.cus_email" default="">
<cfparam name="attributes.cus_hostname" default=""><!--- placeholder only --->
<cfparam name="attributes.cus_browser_type" default="#cgi.http_user_agent#">
<!--- parse out to only pass in value before the "(" 
<cfif isdefined("cgi.http_user_agent") and len(cgi.http_user_agent)>
	<cfset attributes.cus_browser_type=trim(left(cgi.http_user_agent, find("(",cgi.http_user_agent) - 1))>
</cfif>--->
<!--- P03285 end --->

<!--- PAYMENT TYPE --->
<!--- Replace separate credit card attributes with an array :array_ccs_new/array_ccs_orig --->
<cfparam name="attributes.payment_type" default="Credit_Card">
<cfparam name="attributes.pay_type" default="V">
<cfparam name="attributes.array_ccs_new" default="">
<cfparam name="attributes.array_ccs_orig" default="">
<cfparam name="attributes.ponumber" default="">

<!--- BILLING PARAMS --->
<cfparam name="attributes.b_attention" default="">

<!--- SHIPPING PARAMS --->
<cfparam name="attributes.company_name">
<cfparam name="attributes.attention">
<cfparam name="attributes.address1">
<cfparam name="attributes.address2" default="">
<cfparam name="attributes.address3" default="">
<cfparam name="attributes.address4" default="">
<cfparam name="attributes.city">
<cfparam name="attributes.state">
<cfparam name="attributes.country" default="">
<cfparam name="attributes.zip" default="">
<cfparam name="attributes.phone" default="">
<!---SHIPPING CODES  --->
<cfparam name="attributes.code" default="">
<cfparam name="attributes.account_number" default="">
<cfparam name="attributes.ship_price" default="">
<cfparam name="attributes.tax_exemption_requested" default="">
<!--- SYSTEM NOTES --->
<cfparam name="attributes.packing_slip_notes" default="">
<cfparam name="attributes.invoice_notes" default="">
<cfparam name="attributes.management_notes" default="">
<!--- ARRAYS SYSTEM AND LINITEM  --->
<cfparam name="attributes.array_parts" default="">
<cfparam name="attributes.array_systems" default="">
<cfparam name="attributes.cart" default="">

<!--- BEGIN TO BUILD THE XML POST --->

<cfscript>
// Create the payment tags
// info for credit cards existing on the orginal order
payment = "";
if  (isarray(attributes.array_ccs_orig) and arraylen(attributes.array_ccs_orig) GT 0) {
	indexcnt_orig = arraylen(attributes.array_ccs_orig) + 1;
	for ( i=1; i lt indexcnt_orig ; i=i+1) {
		payment = payment & '         
			<payment type="#attributes.payment_type#">
			<ccln>#ucase(attributes.array_ccs_orig[i].cc_line)#</ccln>
			<partial_ccn>#ucase(attributes.array_ccs_orig[i].partial_num)#</partial_ccn>
			<amount>#ucase(attributes.array_ccs_orig[i].amount)#</amount>
			<expr_date>#ucase(attributes.array_ccs_orig[i].expiration)#</expr_date>
			<po_number>#ucase(attributes.ponumber)#</po_number>
		</payment>         
      	';
	}
}
else if (len(attributes.payment_type) GT 0){
	payment = '
    <payment type="#attributes.payment_type#">
      <ccln></ccln>
      <partial_ccn></partial_ccn>
      <amount></amount>
      <expr_date></expr_date>
	  <po_number>#ucase(attributes.ponumber)#</po_number>
     </payment>         
      ';
}

// info for credit cards added on the post
new_payment="";
if  (isarray(attributes.array_ccs_new) and arraylen(attributes.array_ccs_new) GT 0) {
	indexcnt_new = arraylen(attributes.array_ccs_new) + 1;
	for ( j=1; j lt indexcnt_new ; j=j+1) {
      	new_payment = new_payment & '
		<payment type="#attributes.payment_type#">
			<card_number>#ucase(attributes.array_ccs_new[j].cc_num)#</card_number>
			<expr_date>#ucase(attributes.array_ccs_new[j].cc_exp)#</expr_date>
			<amex_num>#ucase(attributes.array_ccs_new[j].amex_num)#</amex_num>
			<po_number>#ucase(attributes.ponumber)#</po_number>
			<amount>#ucase(attributes.array_ccs_new[j].cc_amt)#</amount>
			<security_code>#ucase(attributes.array_ccs_new[j].cc_security)#</security_code>
		</payment>	      
      	';
	}
}
else if (len(attributes.payment_type) GT 0){
	new_payment = '
    <payment type="#attributes.payment_type#">
      		<card_number></card_number>
			<expr_date></expr_date>
			<amex_num></amex_num>
			<po_number>#ucase(attributes.ponumber)#</po_number>
			<amount></amount>
			<security_code></security_code>
     </payment>         
      ';
}

</cfscript>

<cfif isarray(attributes.array_parts) OR isarray(attributes.array_systems)>
<cfscript>
cart = "";
subtotal = 0;
// Start with standalone parts
if  (isarray(attributes.array_parts) and arraylen(attributes.array_parts) GT 0) {
	indexcnt = arraylen(attributes.array_parts) + 1;
	for ( i=1; i lt indexcnt ; i=i+1) {
      cart = cart & '
         <line_item>
            <product>
               <product_code>#ucase(attributes.array_parts[i].product)#</product_code>';
			   if (attributes.array_parts[i].type IS "original"){
               cart = cart & '
               <ln>#ucase(attributes.array_parts[i].line)#</ln>';
               }
			   cart = cart & '
               <quantity>#ucase(attributes.array_parts[i].quantity)#</quantity>
               <price>#ucase(attributes.array_parts[i].price)#</price>
			   <rsn_code_prc>#ucase(attributes.array_parts[i].price_reason_code)#</rsn_code_prc>
               <bid>#ucase(attributes.array_parts[i].bid)#</bid>
               <bid_exp>#ucase(attributes.array_parts[i].bid_exp)#</bid_exp>
            </product>
         </line_item>';
	}
}

// Now the systems 
if  (isarray(attributes.array_systems) and arraylen(attributes.array_systems)) {
	indexcnt1 = arraylen(attributes.array_systems) + 1;  
 //writeoutput(arraylen(attributes.array_systems));
	for (x=1; x lt indexcnt1; x=x+1) {
      cart = cart & '
         <line_item>
             <configuration>';
                if (attributes.array_systems[x].type IS "original"){
                cart = cart & '
                 <ln>#ucase(attributes.array_systems[x].line)#</ln>';
                }		   
	             cart = cart & '
               <quantity>#ucase(attributes.array_systems[x].quantity)#</quantity>
               <baseunit>
                  <product_code>#ucase(attributes.array_systems[x].b_product)#</product_code>
                  <price>#ucase(attributes.array_systems[x].b_price)#</price>
                  <bid>#ucase(attributes.array_systems[x].bid)#</bid>
                  <bid_exp>#ucase(attributes.array_systems[x].bid_exp)#</bid_exp>
                  <rsn_code_prc>#ucase(attributes.array_systems[x].b_price_reason_code)#</rsn_code_prc>
               </baseunit>';
		indexcnt2 = arraylen(attributes.array_systems[x].subitem) + 1; 					
	//writeoutput(arraylen(attributes.array_systems[x].subitem));
	for (y=1; y lt indexcnt2;  y=y+1) {
      cart = cart & '
                  <component>';
	                 cart = cart & '
                  <product_code>#ucase(attributes.array_systems[x].subitem[y].product)#</product_code>
                  <quantity>#ucase(attributes.array_systems[x].subitem[y].quantity)#</quantity>
                  <price>#ucase(attributes.array_systems[x].subitem[y].price)#</price>
                  <rsn_code_prc>#ucase(attributes.array_systems[x].subitem[y].price_reason_code)#</rsn_code_prc>
               </component>';
		}
		cart = cart & '            
            </configuration>
         </line_item>';
	}
}
</cfscript>
<cfelse>
	<cfset cart = attributes.cart>
</cfif>

<cfscript>
// Now build the rest of the xml to post to middle ware. KEEP FORMATING SAME AS BELOW...
xml = '<!DOCTYPE copy SYSTEM "#attributes.dtd#">
<xtiinput>
   <inputheader>
      <actionset>
         <action>chg_order</action>
      </actionset>
   </inputheader>
   <inputdata>
      <customerid>#ucase(attributes.customerid)#</customerid>
      <contactid>#ucase(attributes.contactid)#</contactid>
      <order_number>#ucase(attributes.order_number)#</order_number>
      <order_header>
         <misc_code_1>#ucase(attributes.misc_code_1)#</misc_code_1>
         <misc_amt_1>#ucase(attributes.misc_amt_1)#</misc_amt_1>
         <misc_code_2>#ucase(attributes.misc_code_2)#</misc_code_2>
         <misc_amt_2>#ucase(attributes.misc_amt_2)#</misc_amt_2>
         <source>#attributes.source#</source>
         <ship_complete></ship_complete>
         <tsrid>#attributes.tsrid#</tsrid>
		 <tsr_taker>#attributes.tsr_taker#</tsr_taker>
         <user>#attributes.user#</user>
         <client_order></client_order>
         <order_type>#ucase(attributes.order_type)#</order_type>
         <billattention>#xmlformat(UCase(attributes.attention))#</billattention>
         <pay_type>#attributes.pay_type#</pay_type>
		 <cid>#attributes.cid#</cid>
         <locid>#attributes.locid#</locid>
         <program_id>#attributes.program_id#</program_id>
		 <auto_bo>#ucase(attributes.auto_bo)#</auto_bo>
         	 <currency>#trim(attributes.currency)#</currency>
		 <cus_browser_email>#attributes.cus_browser_email#</cus_browser_email>
         <cus_browser_type>#attributes.cus_browser_type#</cus_browser_type>
         <cus_cookies_accepted>#attributes.cus_cookies_accepted#</cus_cookies_accepted>
		 <cus_email>#attributes.cus_email#</cus_email>
         <cus_hostname>#attributes.cus_hostname#</cus_hostname>
         <cus_ip_address>#attributes.cus_ip_address#</cus_ip_address>
		 <tax_exemption_requested>#ucase(attributes.tax_exemption_requested)#</tax_exemption_requested>
      </order_header>
	  #payment#
	  #new_payment#  
	   <billing_addr>
         <attention>#xmlformat(ucase(attributes.b_attention))#</attention>
      </billing_addr>
      <shipping_addr>
         <company_name>#xmlformat(ucase(attributes.company_name))#</company_name>
         <attention>#xmlformat(ucase(attributes.attention))#</attention>
         <address_1>#xmlformat(ucase(attributes.address1))#</address_1>
         <address_2>#xmlformat(ucase(attributes.address2))#</address_2>
         <city>#xmlformat(ucase(attributes.city))#</city>
         <state>#ucase(attributes.state)#</state>
         <zip>#ucase(attributes.zip)#</zip>
         <phone>#ucase(attributes.phone)#</phone>
      </shipping_addr>
      <shipping_codes>
         <method>
            <code>#attributes.code#</code>
            <account_number></account_number>
            <price>#attributes.ship_price#</price>
		 </method>
      </shipping_codes>
      <system_notes>
         <packing_slip_notes>#xmlformat(ucase(attributes.packing_slip_notes))#</packing_slip_notes>
         <invoice_notes>#xmlformat(ucase(attributes.invoice_notes))#</invoice_notes>
         <management_notes>#xmlformat(ucase(attributes.management_notes))#</management_notes>
      </system_notes>
      <shopping_cart>
	  #cart#
      </shopping_cart>
   </inputdata>
</xtiinput>';
</cfscript>